﻿var tryTime, timeout, randomMin, randomMax, coursewareId, coursewareName;
var validHandle, randomHandle, learningHandle,
    currentTryTime, currentTimeout, currentRandomValue, currentRandomTotal, totals = 0;
var validTotals = 0;
function validInterval() {
    currentTimeout--;
    if (currentTimeout <= 0) {
        stopValidInterval();
        $('#validate_window').modal('hide');
        $('#b_signout').remove();
        $("#totalTime").html('00:00:00');
        totals = 0;
        fx.alert('您已超过验证时间，学习将不计时。');
    } else {
        $('#validate_vtitle').html(currentTimeout);
    }
};

function startValidInterval() {
    if (!validHandle) {
        stopRandomInterval();
        currentTryTime = tryTime;
        currentTimeout = timeout;
        validHandle = window.setInterval(function () { validInterval(); }, 1000);
    }
};

function stopValidInterval() {
    if (validHandle) {
        window.clearInterval(validHandle);
        validHandle = null;
    }
};

function valid(validType, callback) {
    fx.window({
        url: '/Student/LearningValidateModal',
        show: function () {

            startValidInterval();

            $('.validate_window_close').click(function () {
                stopValidInterval();
                $('#validate_window').modal('hide');
                $('#b_signout').remove();
                $("#totalTime").html('00:00:00');
                totals = 0;
                fx.alert('学习将不计时！');
            });

            $('.btn-answer').click(function () {
                var validateId = $('#validateId').val();
                var answer = $(this).data('value');
                fx.ajax({
                    maskTarget: 'body',
                    delay: 200,
                    url: '/Student/LearningValidate',
                    data: {
                        validateId: validateId,
                        coursewareId: coursewareId,
                        answer: answer,
                        validTotals:validTotals,
                        recordType: validType
                    },
                    success: function (result) {
                        $('body').unmask();
                        if (result.success) {
                            $('#validate_window').modal('hide');
                            stopValidInterval();
                            if (callback) {
                                callback();
                            }
                            validTotals = 0;
                        } else {
                            currentTryTime--;
                            if (currentTryTime <= 0) {
                                stopValidInterval();
                                $('#validate_window').modal('hide');
                                $('#b_signout').remove();
                                $("#totalTime").html('00:00:00');
                                totals = 0;
                                fx.alert('回答问题达到最大次数,学习将不计时！');
                            } else {
                                $('#validate_window').modal('hide');
                                valid(validType, callback);
                            }
                        }
                    }
                });
            });
        }
    });
};



function getRandomNumber(min, max) {
    return parseInt(Math.random() * (max - min + 1) + min);
};

function randomInterval(callback) {
    if (currentRandomValue >= currentRandomTotal) {
        stopRandomInterval();
        stopLearningInterval();
        valid(3, callback);
    } else {
        currentRandomValue++;
    }
}

function startRandomInterval(callback) {
    if (!randomHandle) {
        currentRandomTotal = getRandomNumber(randomMin, randomMax);
        currentRandomValue = 0;
        randomHandle = window.setInterval(function () { randomInterval(callback); }, 1000);
    }
}

function stopRandomInterval() {
    if (randomHandle) {
        window.clearInterval(randomHandle);
        randomHandle = null;
    }
}



function learningInterval() {
    totals += 1;
    validTotals += 1;

    var _tos = totals;
    var hour = Math.floor(_tos / 3600);
    if (hour < 1) {
        hour = 0;
    }
    _tos = _tos - (hour * 3600);
    var min = Math.floor(_tos / 60);
    if (min < 1) {
        min = 0;
    }
    _tos = _tos - (min * 60);
    var sec = _tos % 60; // 这是取模

    if (hour < 10) {
        hour = '0' + hour;
    }

    if (min < 10) {
        min = '0' + min;
    }

    if (sec < 10) {
        sec = '0' + sec;
    }

    $("#totalTime").html(hour + ':' + min + ':' + sec);
}

function startLearningInterval() {
    if (!learningHandle) {
        learningHandle = window.setInterval(function () { learningInterval(); }, 1000);
        startRandomInterval(function () {
            startLearningInterval();
        });
    }
}

function stopLearningInterval() {
    if (learningHandle) {
        window.clearInterval(learningHandle);
        learningHandle = null;
    }
}


function begin() {
    fx.ajax({
        masked: false,
        url: '/StudentLearning/Begin',
        data: { coursewareId: coursewareId },
        success: function (result) {
            if (result.success) {
                startLearningInterval();
            } else {
                fx.alert(result.message);
            }
        }
    });
}


function commit() {
    var period = validTotals;
    fx.ajax({
        maskTarget: 'body',
        delay: 200,
        url: '/StudentLearning/Commit',
        data: { period: period },
        success: function (result) {
            if (result.success) {
                fx.alert('学时保存成功！', function (i) {
                    layer.close(i);
                    window.location.href = '/default/index';
                });
            } else {
                fx.alert(result.message);
            }
        }
    });
}

function checkPeriod() {
    fx.ajax({
        maskTarget: 'body',
        delay: 200,
        url: '/StudentLearning/CheckPeriod',
        data: { coursewareId: coursewareId },
        success: function (result) {
            fx.unmask({ maskTarget: 'body' });
            if (result.success) {
                valid(1, begin);
            } else {
                $('#b_signout').remove();
                $('#totalTime').html(result.message);
                //fx.alert(result.message);
            }
        }
    });
}

function loadNote() {
    fx.ajax({
        maskTarget: '.notelist',
        delay: 200,
        dataType: 'html',
        url: '/Student/LessonNoteLi',
        success: function (result) {
            $('.notelist').empty().html(result);
        }
    });
};

function saveNote() {
    var msg = $('#note_message').val();
    if (msg == '') {
        fx.alert('请输入笔记内容！');
        return;
    }
    if (msg.length > 200) {
        fx.alert('笔记长度不能超过200个字符！');
        return;
    }
    fx.ajax({
        maskTarget: '.notelist',
        delay: 200,
        url: '/Student/LessonNoteSave',
        data: { message: msg, coursewareId: coursewareId, coursewareName: coursewareName },
        success: function () {
            fx.unmask({ maskTarget: '.notelist' });
            $('#note_message').val('');
            $('#note_message').focus();
            loadNote();
        }
    });
};

function init(p_tryTime, p_timeout, p_randomMin, p_randomMax, p_coursewareId, p_coursewareName) {
    tryTime = p_tryTime;
    timeout = p_timeout;
    randomMin = p_randomMin;
    randomMax = p_randomMax;
    coursewareId = p_coursewareId;
    coursewareName = p_coursewareName;

    $(function () {

        $('#b_fullscreen').click(function () {
            $('.galleria').data('galleria').enterFullscreen();
        });

        $('#b_signout').click(function () {
            stopLearningInterval();
            valid(2, commit);
        });

        $('#notepop').on('shown.bs.modal', function () {
            loadNote();
        });

        $('#save_note').click(function () {
            saveNote();
        });

        $('#cancel_note').click(function () {
            $('#notepop').popoverX('hide');
        });

        checkPeriod();
    });
}
